<?php
namespace BP\GPM;
require_once("java/security/MessageDigest.php");
class Crypto {
	public static function EncryptString ($encryptString) // [String encryptString]
	{
		$strEncrypt = $encryptString;
		if ($DataType->IsNullOrEmpty($encryptString))
		{
			return $encryptString;
		}
		$strEncrypt = Crypto::MD5_Encrypt($strEncrypt);
		$strEncrypt = Crypto::MD5_Encrypt($strEncrypt);
		return $strEncrypt->toUpperCase();
	}
	public static function MD5_Encrypt ($encryptString) // [String encryptString]
	{
		$md5 = NULL;
		try 
		{
			$md5 = $MessageDigest->getInstance("MD5");
		}
		catch (Exception $e)
		{
			print ( $e . "\n");
			$e->printStackTrace();
			return "";
		}
		$charArray = $encryptString->toCharArray();
		$byteArray = array();
		for ($i = 0; ($i < count($charArray) /*from: charArray.length*/); ++$i) 
			$byteArray[$i] = $charArray[$i];
		$md5Bytes = $md5->digest($byteArray);
		$hexValue = ''; //
		for ($i = 0; ($i < count($md5Bytes) /*from: md5Bytes.length*/); ++$i) 
		{
			$val = (($md5Bytes[$i]) & 0xff);
			if (($val < 16))
				$hexValue->append("0");
			$hexValue->append($Integer->toHexString($val));
		}
		return $hexValue;
	}
	public static function SHA1_Encrypt ($encryptString) // [String encryptString]
	{
		if ((($encryptString . NULL) . ($encryptString->length() == 0)))
		{
			return NULL;
		}
		$hexDigits = '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f';
		try 
		{
			$mdTemp = $MessageDigest->getInstance("SHA1");
			$mdTemp->update($encryptString->getBytes("UTF-8"));
			$md = $mdTemp->digest();
			$j = count($md) /*from: md.length*/;
			$buf = array();
			$k = 0;
			for ($i = 0; ($i < $j); ++$i) 
			{
				$byte0 = $md[$i];
				$buf[++$k] = $hexDigits[(($byte0 >>> 4) & 0xf)];
				$buf[++$k] = $hexDigits[($byte0 & 0xf)];
			}
			return new String($buf);
		}
		catch (Exception $e)
		{
			return NULL;
		}
	}
}
?>
